package com.ztspeech;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;

import org.dragonnova.meetingapp.utils.LogUtil;

import java.io.InputStream;

/**
 * 紫东翻译接口
 * Created by Administrator on 2015/8/5 11:10.
 * Author: Songxy
 * Email: thinkdata@163.com
 */
public class ZTTranslate extends AZTClient {

    private final static String TAG = ZTTranslate.class.getName();

    private final static String SERVICETYPE = "t2t";

    private String language;
    public static final String LANGUAGE_MANDARIN = "chen";
    public static final String LANGUAGE_ENGLISH = "ench";

    private String TranslateText;

    private Thread currentThread;

    private Handler mHandler;

    public ZTTranslate(Handler hanlder) {
        this.mHandler = hanlder;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    @Override
    protected String formatParam() {
        if (this.language == null)
            this.language = LANGUAGE_ENGLISH;

        return String.format("spd=%f&t=%s&io=%s&sc=opu", speed, SERVICETYPE, language);
    }

    @Override
    protected String doResponse(@NonNull InputStream input) throws Exception {
        return this.doCommonResponse(input);
    }

    public String start(String orgContent) {
        try {
            return (String) this.doPost(orgContent);
        } catch (Exception e) {
        }
        return null;
    }

    public synchronized boolean startTranslate(String orgContent) {
        this.TranslateText = orgContent;
        Thread thread = new Thread(TranslateRunnable);
        currentThread = thread;
        thread.start();
        return true;
    }

    /**
     * from hucn: 翻译的线程
     */
    private Runnable TranslateRunnable = new Runnable() {
        @Override
        public void run() {
            Thread thisThread = Thread.currentThread();
            synchronized (this) {
                if (thisThread == ZTTranslate.this.currentThread) {
                    try {
                        LogUtil.e(TAG, "translating........");
                        String result = (String) ZTTranslate.this.doPost(TranslateText);
                        Message msg = mHandler.obtainMessage(AppAudio.TRANSLATED);
                        Bundle bundle = new Bundle();
                        bundle.putString("orgTxt", TranslateText);
                        bundle.putString("tranTxt", result);
                        bundle.putBoolean("isCH2EN", language.equals(LANGUAGE_MANDARIN));
                        msg.setData(bundle);
                        if (thisThread == ZTTranslate.this.currentThread) {
                            mHandler.sendMessage(msg);
                        }
                        LogUtil.e(TAG, "translated........");
                    } catch (Exception e) {
                        e.printStackTrace();
                        Message msg = mHandler.obtainMessage(AppAudio.TRANSLATE_ERROR);
                        mHandler.sendMessage(msg);
                    }
                }
            }
        }
    };

    /**
     * from hucn: 停止
     */
    public void stopTranslate() {
        currentThread = null;
    }
}
